package com.hongning.railway.ui.main.mine.score;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.databinding.ObservableList;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.reqbean.ReqScoreBean;
import com.hongning.railway.entity.resbean.ResScoreRankBean;
import com.hongning.railway.entity.resbean.ResScoreRankInfo;
import com.hongning.railway.ui.main.mine.score.itemviewmodel.RankItemViewModel;
import com.hongning.railway.util.Apputils;

import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class RankTypeFragmentViewModel extends BaseViewModel<AppRepository>{
    public ObservableField<String> myname = new ObservableField<>();
    public ObservableField<String> rankinfo = new ObservableField<String>();
    public ObservableField<String> score = new ObservableField<String>();
    public ItemBinding<RankItemViewModel> item = ItemBinding.of(BR.viewModel,R.layout.item_rank);
    public ObservableList<RankItemViewModel> viewlist = new ObservableArrayList<>();
    public RankTypeFragmentViewModel(@NonNull Application application) {
        super(application);
    }


    public RankTypeFragmentViewModel(@NonNull Application application, AppRepository model) {
        super(application, model);
    }

    public void setHeadInfo(ResScoreRankInfo resScoreRankInfo){
        rankinfo.set("排名：第"+resScoreRankInfo.getTotleRank()+"名");
        if (TextUtils.isEmpty(resScoreRankInfo.getSumScore())){
            score.set("积分：0");
        }else {
            score.set("积分："+resScoreRankInfo.getSumScore());
        }

        myname.set(Apputils.getLoginInfo().getUserName());
    }

    public void loaddata(String date){
        ReqScoreBean reqScoreBean = new ReqScoreBean();
        reqScoreBean.setDate(date);
        reqScoreBean.setStaffId(Apputils.getStaffid());
        model.allrankScore(reqScoreBean)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new ApiDisposableObserver<List<ResScoreRankBean>>() {
                    @Override
                    public void onResult(List<ResScoreRankBean> resScoreRankBeans) {
                        for (ResScoreRankBean data:resScoreRankBeans) {
                            viewlist.add(new RankItemViewModel(RankTypeFragmentViewModel.this,data));
                        }
                    }
                });
    }

    public void loadzhibudata(String date) {
        ReqScoreBean reqScoreBean = new ReqScoreBean();
        reqScoreBean.setDate(date);
        reqScoreBean.setStaffId(Apputils.getStaffid());
        model.zhiburankScore(reqScoreBean)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new ApiDisposableObserver<List<ResScoreRankBean>>() {
                    @Override
                    public void onResult(List<ResScoreRankBean> resScoreRankBeans) {
                        for (ResScoreRankBean data:resScoreRankBeans) {
                            viewlist.add(new RankItemViewModel(RankTypeFragmentViewModel.this,data));
                        }
                    }
                });
    }

    public void setzhibuHeadInfo(ResScoreRankInfo resScoreRankInfo) {
        rankinfo.set("排名：第"+resScoreRankInfo.getGroupRank()+"名");
        if (TextUtils.isEmpty(resScoreRankInfo.getSumScore())){
            score.set("积分：0");
        }else {
            score.set("积分："+resScoreRankInfo.getSumScore());
        }

        myname.set(Apputils.getLoginInfo().getUserName());
    }

    public void loadHnData(String date) {
        ReqScoreBean reqScoreBean = new ReqScoreBean();
        reqScoreBean.setDate(date);
        reqScoreBean.setStaffId(Apputils.getStaffid());
        model.allHnrankScore(reqScoreBean)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new ApiDisposableObserver<List<ResScoreRankBean>>() {
                    @Override
                    public void onResult(List<ResScoreRankBean> resScoreRankBeans) {
                        for (ResScoreRankBean data:resScoreRankBeans) {
                            viewlist.add(new RankItemViewModel(RankTypeFragmentViewModel.this,data));
                        }
                    }
                });

    }

    public void loadzhibuHndata(String date) {
        ReqScoreBean reqScoreBean = new ReqScoreBean();
        reqScoreBean.setDate(date);
        reqScoreBean.setStaffId(Apputils.getStaffid());
        model.zhibuHnrankScore(reqScoreBean)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new ApiDisposableObserver<List<ResScoreRankBean>>() {
                    @Override
                    public void onResult(List<ResScoreRankBean> resScoreRankBeans) {
                        for (ResScoreRankBean data:resScoreRankBeans) {
                            viewlist.add(new RankItemViewModel(RankTypeFragmentViewModel.this,data));
                        }
                    }
                });
    }
}
